home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-01-14 | 7.3 KB | 351 lines | [TEXT/CWIE] |
- /*
- File: SimpleParseUtils.cp
-
- Contains: Utilities for useful for very simple
- parsing tasks.
-
- Version: 1.0.2 (for System 7.x)
-
- 1.0.1 - 2 December 1995 - Added StringHasExtension.
- 1.0.2 - 7 January 1996 - Added PtrAppendStr and CollapsePtrWhiteSpace.
- 1.0.3 - 13 January 1996 - Added AddDotExtension.
-
- Copyright: ©1995 Chris K. Thomas. All Rights Reserved.
- */
-
- #ifndef Assert_
- #define Assert_(x) if(!x) DebugStr("\p AssertionFailed: " #x);
- #define ThrowIfNULL_(x)
- #define ThrowIfMemFail_(x)
- #endif
-
- #include "SimpleParseUtils.h"
-
- // * if inMem points to the same string as inString, true
- Boolean StrEquals(const unsigned char *inString, char *inMem)
- {
- Boolean out = true;
-
- for(long i = 1; i <= inString[0]; i++)
- {
- if(*(inString + i) != *(unsigned char *)(inMem + i))
- {
- out = false;
- break;
- }
- }
-
- return out;
- }
-
- // * if inMem points to a the same string as inString followed by whitespace, true
- Boolean StrEqualsToken(const unsigned char *inString, char *inMem)
- {
- Boolean out = true;
-
- for(long i = 1; i <= inString[0]; i++)
- {
- if(*(inString + i) != *(unsigned char *)(inMem + i))
- {
- out = false;
- break;
- }
- }
-
- // * if the character after the string isn’t whitespace, false
- if(out)
- out = StrEqualsChars(whiteSpaces, (char *)&inMem[inString[0] + 1]);
-
- return out;
- }
-
- // * if inMem points to a the same string as inString followed by whitespace, true
- Boolean PtrEqualsToken(const Ptr inString, char *inMem)
- {
- Assert_(inString);
- Boolean out = true;
-
- long strSize = GetPtrSize(inString);
-
- for(long i = 0; i < strSize; i++)
- {
- if(*(inString + i) != *(unsigned char *)(inMem + i))
- {
- out = false;
- break;
- }
- }
-
- // * if the character after the string isn’t wordbreak, false
- if(out)
- out = StrEqualsChars(wordBreaks, (char *)&inMem[ strSize ]);
-
- return out;
- }
-
- // * if inMem points to a char which is in inTryChars, true
- Boolean StrEqualsChars(const unsigned char *inTryChars, char *inMem)
- {
- Assert_(inTryChars);
- Assert_(inMem);
-
- Boolean out = false;
-
- for(long i = 1; i <= inTryChars[0];i++)
- {
- if(*(inTryChars + i) == *(unsigned char *)inMem)
- {
- out = true;
- break;
- }
- }
-
- return out;
- }
-
- // * count whitespace chars
- long CountWhiteSpace(char *inMem, long inLength)
- {
- Assert_(inMem);
-
- long out = 0;
-
- while(StrEqualsChars(whiteSpaces, inMem + out)
- && (out < inLength))
- {
- out++;
- }
-
- return out;
- }
-
- // * return a string containing the next token
- StringPtr GetTokenString(char *inMem, long inLength)
- {
- Assert_(inMem);
-
- StringPtr curString;
- long wordLength = 0;
- long startIndex;
-
- startIndex = CountWhiteSpace(inMem, inLength);
-
- while(!StrEqualsChars(wordBreaks, (inMem + startIndex + wordLength)))
- wordLength++;
-
- curString = (StringPtr)NewPtr(wordLength + 1);
- ThrowIfNULL_(curString);
-
- BlockMoveData(inMem + startIndex, &curString[1], wordLength);
- curString[0] = wordLength;
-
- return curString;
- }
-
- // * return a length-unlimited string containing the next token
- Ptr GetTokenPtr(char *inMem, long inLength)
- {
- Assert_(inMem);
-
- Ptr curString;
- long wordLength = 0;
- long startIndex;
-
- startIndex = CountWhiteSpace(inMem, inLength);
-
- while(!StrEqualsChars(wordBreaks, (inMem + startIndex + wordLength)))
- wordLength++;
-
- curString = NewPtr(wordLength);
- ThrowIfMemFail_(curString);
-
- BlockMoveData(inMem + startIndex, &curString[0], wordLength);
-
- return curString;
- }
-
- Ptr GetDelimitedToken(const unsigned char *inDelimiter, char *inMem, long inLength)
- {
- Assert_(inMem);
-
- Ptr curString;
- long wordLength = 0;
- long startIndex;
-
- startIndex = CountWhiteSpace(inMem, inLength);
-
- while(!StrEqualsChars(inDelimiter, (inMem + startIndex + wordLength)))
- wordLength++;
-
- curString = NewPtr(wordLength);
- ThrowIfMemFail_(curString);
-
- BlockMoveData(inMem + startIndex, &curString[0], wordLength);
-
- return curString;
- }
-
- // * return the char in inString actually found
- short FindSelectChar(const unsigned char *inString, char *inMem, long inLength)
- {
- long i = 0;
- short out = 0;
-
- while(i < inLength)
- {
-
- // * compare the current char in inMem with each char in inString
- for(long charIndex = 1; charIndex <= inString[0]; charIndex++)
- {
- if(inMem[i] == inString[charIndex])
- {
- out = inString[charIndex];
-
- // * break out of the outer loop
- // * without requiring a doneFlag
- i = inLength + 5; // * could be any value
- break;
- }
- }
-
- i++;
- }
-
- return out;
- }
-
- // * get all text from inMem to end of line
- Ptr GetEntireLine(char *inMem, long inLength)
- {
- long lineLength = 0;
- Ptr out = NULL;
-
- while(!StrEqualsChars(endLines, &inMem[lineLength]) && lineLength < inLength)
- lineLength++;
-
- out = NewPtr(lineLength);
- ThrowIfMemFail_(out);
-
- BlockMoveData(&inMem[0], out, lineLength);
-
- return out;
- }
-
- // * for checking dot extensions (.c .h .pas .r, etc)
- Boolean
- StringHasExtension(const unsigned char * inExtension, const unsigned char *inString)
- {
- Boolean out = true;
- long extensionIndex = inExtension[0];
- long stringIndex = inString[0];
-
- //
- // if the extension is bigger than the string
- // the string can’t have the extension
- //
- if(inExtension[0] <= inString[0])
- {
- for(long index = inExtension[0]; extensionIndex > 0; )
- {
- if(inString[stringIndex] != inExtension[extensionIndex])
- {
- out = false;
- break;
- }
-
- extensionIndex--;
- stringIndex--;
- }
- }
- else
- {
- out = false;
- }
-
- return out;
- }
-
- // * append a pstring to the given Ptr
- void
- PtrAppendStr(Ptr inDestPtr, unsigned char *inStringToAppend)
- {
- long formerLength = GetPtrSize(inDestPtr);
-
- SetPtrSize(inDestPtr, formerLength + *inStringToAppend);
- if(MemError() != noErr)
- throw MemError();
-
- BlockMoveData(&inStringToAppend[1], &inDestPtr[formerLength], *inStringToAppend);
- }
-
- // * strip all but one space from contiguous runs of whitespace; can shrink inDestPtr
- void CollapsePtrWhiteSpace(Ptr inDestPtr)
- {
- long size = GetPtrSize(inDestPtr);
- long currentSpace;
-
- for(long index = 0; index < size; index++)
- {
- currentSpace = CountWhiteSpace(&inDestPtr[index], size);
-
- if(currentSpace > 0)
- {
- // shift the bytes down to where we only have one space
- BlockMoveData(&inDestPtr[index + currentSpace], &inDestPtr[index + 1], size - (index + currentSpace - 1));
-
- // make it a real space, not a tab or CR/LF
- inDestPtr[index] = ' ';
-
- // shrink ptr size
- size -= (currentSpace - 1);
- SetPtrSize(inDestPtr, size);
- if(MemError() != noErr)
- throw MemError();
- }
- }
- }
-
- // * look for inFindChar until either we hit a char in inUntil or index >= inLength
- Boolean FindCharUntil(short inFindChar, const unsigned char *inUntil, char *inMem, long inLength)
- {
- Boolean out = false; //pessimism
-
-
- // DebugStr((unsigned char *)(inMem - 1));
- for(long index = 0; index < inLength; index++)
- {
- if(inFindChar == inMem[index])
- {
- // success!
- out = true;
- break;
- }
- else if(StrEqualsChars(inUntil, &inMem[index]))
- {
- // found an "until" char- failure
- break;
- }
-
- }
-
- return out;
- }
-
- // * add a .dot extension to a Mac filename (yuk!)
- void AddDotExtension(const Str255 inString, const Str255 inDot, Str255 outString)
- {
- if(inString[0] + inDot[0] >= 31)
- {
- BlockMoveData(&inString[1], &outString[1], 31 - inDot[0]);
- BlockMoveData(&inDot[1], &outString[31 - inDot[0]], inDot[0]);
- outString[0] = 31;
- }
- else
- {
- BlockMoveData(&inString[1], &outString[1], inString[0]);
- BlockMoveData(&inDot[1], &outString[ inString[0] + 1], inDot[0]);
- outString[0] = inDot[0] + inString[0];
- }
- }
-
-